package com.zishi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zishi.model.pojo.Article;
import com.zishi.model.pojo.UserArticleRef;
import com.zishi.mapper.UserArticleRefMapper;
import com.zishi.service.UserArticleRefService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户点赞和收藏的文章表 服务实现类
 * </p>
 *
 * @author TP
 * @since 2022-05-05 08:53:41
 */
@Service
public class UserArticleRefServiceImpl extends ServiceImpl<UserArticleRefMapper, UserArticleRef> implements UserArticleRefService {


    @Autowired
    private UserArticleRefMapper userArticleRefMapper;

    @Autowired
    private ArticleServiceImpl articleService;

    @Override
    public List<Article> getBatchUserArticle(int userId) {

        return userArticleRefMapper.getBatchUserArticle(userId);
    }

    @Override
    public boolean userLikeArticle(Integer userId, Integer articleId) {
        // 该记录是否存在
        QueryWrapper<UserArticleRef> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("article_id", articleId);
        UserArticleRef one = userArticleRefMapper.selectOne(wrapper);

        // 更新文章点赞数
        articleService.updateLikeNum(articleId, 1);
        if (one != null) {
            return userArticleRefMapper.updateLikeOrCollection(userId, articleId, 1, null);
        }

        return userArticleRefMapper.insert(new UserArticleRef(userId, articleId, 1, null)) > 0;
    }

    @Override
    public boolean userCollectArticle(Integer userId, Integer articleId) {
        // 该记录是否存在
        QueryWrapper<UserArticleRef> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("article_id", articleId);
        UserArticleRef one = userArticleRefMapper.selectOne(wrapper);

        // 更新文章收藏数
        articleService.updateCollectNum(articleId, 1);
        if (one != null) {
            return userArticleRefMapper.updateLikeOrCollection(userId, articleId, null, 1);
        }

        return userArticleRefMapper.insert(new UserArticleRef(userId, articleId, null, 1)) > 0;
    }

    @Override
    public boolean userCancelLikeArticle(Integer userId, Integer articleId) {
        QueryWrapper<UserArticleRef> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("article_id", articleId);
        // 更新文章点赞数
        articleService.updateLikeNum(articleId, -1);
        return userArticleRefMapper.update(new UserArticleRef(null, null, 0, null), wrapper) > 0;
    }

    @Override
    public boolean userCancelCollectArticle(Integer userId, Integer articleId) {
        QueryWrapper<UserArticleRef> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("article_id", articleId);
        // 更新文章收藏数
        articleService.updateCollectNum(articleId, -1);
        return userArticleRefMapper.update(new UserArticleRef(null, null, null, 0), wrapper) > 0;
    }
}
